package com.jiang.spring_security_singleboot.mapper;

import com.jiang.spring_security_singleboot.domain.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @中软华腾软件系统有限公司,未经授权不得擅自传播
 * @类描述:
 * @类名称:UserMapper
 * @作者: 蒋正午
 * @编辑时间: 2021/4/26 23:19
 * @版本: 1.0
 */
public interface UserMapper {

    @Select("SELECT u.* FROM `sys_user` u WHERE username=#{username};")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
            many = @Many(select = "com.jiang.spring_security_singleboot.mapper.RoleMapper.findRoleByUid")
            )}
    )
    List<SysUser> findUsersByUsername(String username);

    @Insert("insert into sys_user (username, password) values (#{username}, #{password})")
    public void save(SysUser user);

    @Select("select * from sys_user")
    public List<SysUser> findAll();

    @Select("SELECT r.id FROM sys_role r, sys_user_role ur " +
            "WHERE ur.rid=r.id AND ur.uid=#{uid}")
    public List<Integer> findRolesByUid(Integer id);

    @Delete("delete from sys_user_role where uid=#{userId}")
    public void removeRoles(Integer userId);

    @Insert("insert into sys_user_role values (#{uid}, #{rid})")
    public void addRoles(@Param("uid") Integer userId, @Param("rid") Integer rid);


}
